import zipfile
import os


def zip_dir(dirname, zipfilename):
    filelist = []
    if os.path.isfile(dirname):
        filelist.append(dirname)
    else:
        for root, dirs, files in os.walk(dirname):
            for name in files:
                filelist.append(os.path.join(root, name))

    zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED)
    for tar in filelist:
        arcname = tar[len(dirname) :]
        zf.write(tar, arcname)
    zf.close()


# 解压dir
def unzip_dir(zipfilename, unziptodir):
    if not os.path.exists(unziptodir):
        os.makedirs(unziptodir)
    file_zip = zipfile.ZipFile(zipfilename, "r")
    file_zip.extractall(os.path.abspath(unziptodir))
    file_zip.close()
    os.remove(zipfilename)
